package com.ityy.ruiji.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ityy.ruiji.common.R;
import com.ityy.ruiji.dao.*;
import com.ityy.ruiji.entity.*;
import com.ityy.ruiji.service.OrderService;
import com.ityy.ruiji.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired(required = false)
    private ShoppingCartDao shoppingCartDao;

    @Autowired(required = false)
    private UserDao userDao;

    @Autowired(required = false)
    private AddressBookDao addressBookDao;


    @Autowired(required = false)
    private OrderDetailDao orderDetailDao;


    @Autowired(required = false)
    private OrderDao orderDao;

    @Override
    @Transactional
    public R save(Orders orders) {
        Long userId = orders.getUserId();
        List<ShoppingCart> shoppingCartList = shoppingCartDao.findByUserId(userId);
        //B. 根据当前登录用户id, 查询用户数据
        User user = userDao.selectById(userId);

        //C. 根据地址ID, 查询地址数据
        AddressBook addressBook = addressBookDao.finById(orders.getAddressBookId());

        //我自己生成的订单id
        Integer orderId = UUIDUtils.getUUIDInOrderId();
        List<OrderDetail> orderDetailList = new ArrayList<>();
        BigDecimal total = new BigDecimal("0");
        for (ShoppingCart shoppingCart : shoppingCartList) {
            //每一个购物项就是对应一个订单明细
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(UUIDUtils.getUUIDInOrderId().longValue());
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setOrderId(orderId.longValue());
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber()); //购买的数量
            orderDetail.setAmount(shoppingCart.getAmount()); //商品的单价
            //计算所有商品的总价
            total = total.add(shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber()+"")));

            orderDetail.setImage(shoppingCart.getImage());
            orderDetailList.add(orderDetail); //添加到集合中
        }

        //批量插入订单项
        orderDetailDao.batchInsert(orderDetailList);

        //     E. 组装订单数据, 批量保存订单数据
        orders.setId(Long.valueOf(orderId));  //订单的id
        orders.setNumber(orderId+"");  //订单号
        orders.setStatus(1);  //状态
        orders.setUserId(userId); // 订单所属的用户的id
        orders.setOrderTime(LocalDateTime.now()); //下单时间
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(total); //订单的总金额
        orders.setUserName(user.getName());  //用户名
        orders.setPhone(addressBook.getPhone()); //收货人的手机号
        orders.setAddress(addressBook.getDetail());  //收货人的地址
        orders.setConsignee(addressBook.getConsignee());  //签收人
        orderDao.insert(orders);


//        F. 删除当前用户的购物车列表数据
        shoppingCartDao.cleanShoppingCart(userId);
        return R.success("下单成功");
    }

    @Override
    public R findAll(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
       List<Orders> ordersList = orderDao.findAll();
        PageInfo<Orders> pageInfo = new PageInfo<>(ordersList);
        Page<Orders> ordersPage = new Page<>(pageInfo.getList(),pageInfo.getTotal());
        return R.success(ordersPage);
    }
}
